var powerFactorListData = [];
var deleteFlag = "";

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	search();
});

function search(){
	$ajax('searchPowerFactor', 'powerFactorSearch', searchFactorSearch);
}

function searchFactorSearch(data){
	powerFactorListData = data && data.powerFactorList || [];
	createTableBody("powerFactorTbody", powerFactorTable, powerFactorListData, false, true);
}

function save() {
	deleteFlag = "save";
	addTitle("powerFactorSave");
	allTable();
}
function chkBefore(index, msg){
	deleteFlag = msg;
	$("#ID").val(powerFactorListData[index].id);
	if (deleteFlag == "update") {
		addTitle("powerFactorUpdate");
		allTable(index);
	} else if (deleteFlag == "delete") {
		showMessge(messageContent["Delete.Confirm"], "model2");
	}
}
function allTable(index){
	createComBody("detailOneBody", powerFactorTable, powerFactorListData[index], 1);
	$("#save").modal("show");
}

function getUrl(){
	if(deleteFlag == "save"){
		return 'savePowerFactor';
	} else if(deleteFlag == "update"){
		return 'updatePowerFactor';
	} else if(deleteFlag == "delete"){
		return 'deletePowerFactor';
	}
}

function confirm(){
	return checkForm("saveForm", powerFactorTable);
}

function savePowerFactor(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	     search();
	}else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge(messageContent["Update.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Update.Failed"]);
	     } else{
	    	 showMessge(messageContent["Update.Error"]);
	     }
	     search();
	}
}
function getData(){
	return {ID: $("#ID").val()};
}
function sure(data){
	if(deleteFlag == "delete"){
		var result = data && data.result;
	    if (result == "Success") {
			showMessge(messageContent["Delete.Success"]);
		} else if (result == "NotFound") {
			showMessge(messageContent["Delete.NotFound"]);
		} else {
			showMessge(messageContent["Delete.Error"]);
		}
	    search();
	} 
}